package com.mzy;

import java.util.Arrays;

public class BisectSearch {

    public static int[] lenRandomValueRandom(int maxLen, int maxValue) {
        int len = (int) (Math.random() * maxLen) + 1;
        int[] arr = new int[len];
        for (int i = 0; i < len; i++) {
            arr[i] = ((int) (Math.random() * maxValue));
        }
        return arr;
    }


    public static void main(String[] args) {
        int[] arr = {93, 93, 93, 93};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
        // System.out.println(find(arr, 79));
        System.out.println(findMostLeft(arr, 93));
        // System.out.println(findMostRight(arr, 11));
    }

    private static boolean find(int[] arr, int num) {
        int L = 0;
        int R = arr.length - 1;
        while (L <= R) {
            int mid = (L + R) / 2;
            if (arr[mid] > num) {
                R = mid - 1;
            } else if (arr[mid] < num) {
                L = mid + 1;
            } else {
                return true;
            }
        }
        return false;
    }

    private static int findMostLeft(int[] arr, int num) {

        int L = 0;
        int R = arr.length - 1;
        int index = -1;
        while (L <= R) {
            int mid = (L + R) / 2;
            if (arr[mid] >= num) {
                index = mid;
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return index;
    }


    private static int findMostRight(int[] arr, int num) {

        int L = 0;
        int R = arr.length - 1;
        int index = -1;
        while (L <= R) {
            int mid = (L + R) / 2;
            if (arr[mid] > num) {
                index = mid;
                R = mid - 1;
            } else if (arr[mid] < num) {
                index = mid;
                L = mid + 1;
            } else {
                return mid;
            }
        }
        return index;
    }
}
